package com.lagou.studentsystem.servlet;

import com.lagou.studentsystem.dao.ClassesDao;
import com.lagou.studentsystem.dao.Impl.ClassesDaoImpl;
import com.lagou.studentsystem.model.Classes;
import com.lagou.studentsystem.model.Student;
import com.lagou.studentsystem.service.ClassesService;
import com.lagou.studentsystem.service.StudentService;
import com.lagou.studentsystem.service.impl.ClassesServiceImpl;
import com.lagou.studentsystem.service.impl.StudentServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdateClassesServlet",urlPatterns = "/updateClasses")
public class UpdateClassesServlet extends HttpServlet {

    private ClassesService classesService = new ClassesServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String oldClassesId = request.getParameter("oldClassesId");
        String c_id = request.getParameter("c_id");
        String c_name = request.getParameter("c_name");
        String c_teacher = request.getParameter("c_teacher");
        String c_slogan = request.getParameter("c_slogan");
        String c_grade = request.getParameter("c_grade");
        String c_size = request.getParameter("c_size");
        c_size = c_size.trim();
        System.out.println(c_size);
        Classes classes = new Classes(c_id,c_name,Integer.parseInt(c_grade),c_teacher,c_slogan,Integer.parseInt(c_size));
        int i = classesService.updateClassesById(oldClassesId, classes);
        if (i!=0){
            RequestDispatcher findAllStudent = request.getRequestDispatcher("findClassesAll");
            findAllStudent.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
